package com.xiantm.study.thread.future.p3;

import java.io.FileOutputStream;
import java.io.IOException;

public class Mian {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        Content content1 = Retriever.retrieve("http://www.baidu.com");
        Content content2 = Retriever.retrieve("http://www.qq.com");
        Content content3 = Retriever.retrieve("http://www.sohu.com");
        saveToFile("baidu.html", content1);
        saveToFile("qq.html",content2);
        saveToFile("sohu.html", content3);
        long end = System.currentTimeMillis();
        System.out.println("Elapse time = " + (end - start) + "msec.");
    }

    public static void saveToFile(String fileName,Content content){
        byte[] bytes = content.getBytes();
        try {
            System.out.println(Thread.currentThread().getName() + ": Saving to " + fileName);
            FileOutputStream out = new FileOutputStream(fileName);
            for (int i = 0; i < bytes.length; i++) {
                out.write(bytes[i]);
            }
            out.close();
        }catch (IOException ioException){
            ioException.printStackTrace();
        }
    }

}
